<?php


namespace app\admin\controller;


use app\BaseController;
use app\Request;
use think\facade\Db;
use think\facade\View;

class Homepage extends BaseController
{
    public function index(){//页面
        return View::fetch();
    }
    public function welcome(){//主页
        return View::fetch();
    }
    public function cateList(){//栏目列表
        return View::fetch();
    }
    public function trimall($str){//去除空格
        $oldchar=array(" ","　","\t","\n","\r");
        $newchar=array("","","","","");
        return str_replace($oldchar,$newchar,$str);
    }
    //公告列表
    public function gonggaolieBiao(){
        $time=input('time');
        $title=$this->trimall(input('title'));
        $info = Db::name('i_notice')
            ->where(['delete'=>0])
            ->whereLike('time',"%$time%")->whereLike('title',"%$title%")
            ->order('id desc')->paginate('2');
        $page = $info->render();
        $count = Db::name('i_notice')
            ->where(['delete'=>0])
            ->whereLike('time',"%$time%")
            ->whereLike('title',"%$title%")
            ->count();
        View::assign('count',$count);
        View::assign('page',$page);
        View::assign('info',$info);
        return View::fetch();
    }
    public function gonggaoliebiaoTj(){//公告添加--页面
        return View::fetch();
    }
    public function gonggaoliebiaoInsert(){//公告添加
        $data=[
            'title'=>input('title')
        ];
        $info = Db::name('i_notice')->where('title',$data['title'])->select()->toArray();
        if (!empty($info)){
            $arr=[
                'code'=>102,
                'msg'=>'该数据已存在'
            ];
        }else{
            $infos = Db::name('i_notice')->insert($data);
            if ($infos){
                $arr=[
                    'code'=>100,
                    'msg'=>'添加成功'
                ];
            }else{
                $arr=[
                    'code'=>101,
                    'msg'=>'添加失败'
                ];
            }
        }
        return json($arr);
    }
    public function gonggaoliebiaoXg(){//公告列表-修改-传值
        $id = input('id');
        $info = Db::name('i_notice')->where('id',$id)->select()->toArray();
        View::assign('info',$info);
        return View::fetch();
    }
    public function gonggaoliebiaoUpdata(){//公告列表-修改
        $data = input('post.');
        $info = Db::name('i_notice')->update($data);
        if ($info){
            $arr=[
                'code'=>100,
                'msg'=>'修改成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'请更改信息后再提交哦！'
            ];
        }
        return json($arr);
    }
    public function gonggaoliebiaoQY(){//公告列表-启用-禁用
        $data=[
            'id'=>input('id'),
            'state'=>input('state')?0:1
        ];
        $info = Db::name('i_notice')->update($data);
        if ($info){
            $arr=[
                'code'=>100,
                'msg'=>'操作成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'操作失败'
            ];
        }
        return json($arr);
    }
    public function gonggaoliebiaoDelete(){//公告列表-删除
        $data=[
            'id'=>input('id'),
            'delete'=>input('delete')?0:1
        ];
        $info = Db::name('i_notice')->update($data);
        if ($info){
            $arr=[
                'code'=>100,
                'msg'=>'删除成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'删除失败'
            ];
        }
        return json($arr);
    }
    public function gonggaoliebiaoYsc(){//公告列表--已删除页面
        $info = Db::name('i_notice')->where('delete',1)->order('id desc')->select()->toArray();
        $count = Db::name('i_notice')->where('delete',1)->count();
        View::assign('count',$count);
        View::assign('info',$info);
        return View::fetch();
    }
    public function gonggaoliebiaoDeletes(){//公告列表--已删除页面-恢复
        $data=[
            'id'=>input('id'),
            'delete'=>input('delete')?0:1
        ];
        $info = Db::name('i_notice')->update($data);
        if ($info){
            $arr=[
                'code'=>100,
                'msg'=>'恢复成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'恢复失败'
            ];
        }
        return json($arr);
    }

    //资讯列表
    public function zixunlieBiao(){
        $time=input('time');
        $title=$this->trimall(input('title'));
        $info = Db::name('i_industry')
            ->where(['delete'=>0])
            ->whereLike('time',"%$time%")
            ->whereLike('title',"%$title%")
            ->order('id desc')->paginate('2');
        $page = $info->render();
        $count = Db::name('i_industry')
            ->where(['delete'=>0])
            ->whereLike('time',"%$time%")
            ->whereLike('title',"%$title%")
            ->count();
        View::assign('count',$count);
        View::assign('page',$page);
        View::assign('info',$info);
        return View::fetch();
    }
    public function zixunliebiaoTj(){//资讯添加--页面
        return View::fetch();
    }
    public function zixunliebiaoInsert(){//资讯添加
        $data=[
            'title'=>input('title')
        ];
        $info = Db::name('i_industry')->where('title',$data['title'])->select()->toArray();
        if (!empty($info)){
            $arr=[
                'code'=>102,
                'msg'=>'该数据已存在'
            ];
        }else{
            $infos = Db::name('i_industry')->insert($data);
            if ($infos){
                $arr=[
                    'code'=>100,
                    'msg'=>'添加成功'
                ];
            }else{
                $arr=[
                    'code'=>101,
                    'msg'=>'添加失败'
                ];
            }
        }
        return json($arr);
    }
    public function zixunliebiaoXg(){//资讯列表-修改-传值
        $id = input('id');
        $info = Db::name('i_industry')->where('id',$id)->select()->toArray();
        View::assign('info',$info);
        return View::fetch();
    }
    public function zixunliebiaoUpdata(){
        $data = input('post.');
        $info = Db::name('i_industry')->update($data);
        if ($info){
            $arr=[
                'code'=>100,
                'msg'=>'修改成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'请更改信息后再提交哦！'
            ];
        }
        return json($arr);
    }
    public function zixunliebiaoQY(){//资讯列表-启用-禁用
        $data=[
            'id'=>input('id'),
            'state'=>input('state')?0:1
        ];
        $info = Db::name('i_industry')->update($data);
        if ($info){
            $arr=[
                'code'=>100,
                'msg'=>'操作成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'操作失败'
            ];
        }
        return json($arr);
    }
    public function zixunliebiaoDelete(){//资讯列表-删除
        $data=[
            'id'=>input('id'),
            'delete'=>input('delete')?0:1
        ];
        $info = Db::name('i_industry')->update($data);
        if ($info){
            $arr=[
                'code'=>100,
                'msg'=>'删除成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'删除失败'
            ];
        }
        return json($arr);
    }
    public function zixunliebiaoYsc(){//资讯列表--已删除页面
        $info = Db::name('i_industry')->where('delete',1)->order('id desc')->select()->toArray();
        $count = Db::name('i_industry')->where('delete',1)->count();
        View::assign('count',$count);
        View::assign('info',$info);
        return View::fetch();
    }
    public function zixunliebiaoDeletes(){//资讯列表--已删除页面-恢复
        $data=[
            'id'=>input('id'),
            'delete'=>input('delete')?0:1
        ];
        $info = Db::name('i_industry')->update($data);
        if ($info){
            $arr=[
                'code'=>100,
                'msg'=>'恢复成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'恢复失败'
            ];
        }
        return json($arr);
    }
}